<template>
	<view :class="'baseForm'">
		<!-- 选择加工单 -->

		<!-- <view>
			<u--input
				style="
					background-color: #fff;
					margin: 0 15px 5px 15px;
					height: 18px;
					border-radius: 5px;
				"
				v-model="processName"
				class="inputHeight"
				disabled
				@click="processSelect"
				placeholder="请选择工序"
				suffixIcon="arrow-down"
				clearable></u--input>
		</view> -->

		<!-- 搜索&扫码 -->
		<view class="upload-scan" v-if="tableList.length > 0">
			<!-- <view class="searchButton" @click="dropdownOpen = true">
				{{ options1[value1 - 1].label }}
				<u-icon name="arrow-down"></u-icon>
			</view> -->
			<u-search
				v-model="searchText"
				shape="round"
				bgColor="#fff"
				:show-action="true"
				actionText="搜索"
				placeholder="请输入熔炼编号"
				:animation="true"
				:clearabled="true"
				class="search"
				@search="searchList"
				@clear="clearList"
				@custom="searchList"></u-search>
			<!-- <u-icon v-if="value1 === 1" name="scan" size="27px" :bold="false" @click="openScan"></u-icon> -->

			<!-- 原生扫码 https 协议 -->
			<!-- <u-icon
				v-if="value1 === 1"
				name="scan"
				size="27px"
				:bold="false"
				@click="scanHandle"></u-icon> -->
		</view>

		<!-- 当前作业 -->
		<view class="activeHeight" v-if="tableList.length > 0">
			<!-- 列表 -->
			<u-list
				ref="tableScroll"
				@scrolltolower="scrolltolower"
				class="list"
				:height="listHeight"
				lowerThreshold="50px"
				:pagingEnabled="true">
				<u-list-item v-for="(item, index) in tableList" :key="item.id">
					<view class="card">
						<view class="flex record-title">
							<text class="line1">
								生产顺序号：{{ item.workSequence }}
							</text>
							<!-- <view
								v-if="item.status == '1'"
								style="
									padding: 5px 15px 5px 15px;
									border-radius: 5px;
									color: #000;
									font-size: 12px;
									height: 30px;
									line-height: 30px;
									background-color: #81b337;
								">
								生产完成
							</view>
							<view
								v-if="item.status == '2'"
								style="
									padding: 5px 15px 5px 15px;
									border-radius: 5px;
									color: #000;
									font-size: 12px;
									height: 30px;
									width: 50px;
									text-align: center;
									line-height: 30px;
									background-color: #fff81d;
								">
								执行中
							</view> -->
						</view>
						<view
							class="flex flex-col justify-between flex-1 h-full record-content-body">
							<view class="record-content">
								<view class="record-item">
									<text>
										熔炼编号：{{ item.processingOrderNumber }}
									</text>
									<text>
										生产批次：{{ item.producePlanCode || "无" }}
									</text>
									<text>
										生产日期：{{
											item.planDate || "无"
										}}
									</text>
								</view>
								<view class="record-item">
									<text>
										产品名称：{{ item.materialName }}
									</text>
									<text class="">
										<text>
											炉号：{{ item.plannedBatch || "无" }}
										</text>
									</text>
								</view>
								<view class="record-item">
									<text>
										计划开始时间： {{ item.planBeginTime }}
									</text>
									<text>计划结束时间{{ item.planEndTime }}</text>
								</view>
								<view class="record-item">
									<text>
										实际开始时间：
										{{ item.beginTime }}
									</text>
									<text>
										实际结束时间： {{ item.endTime }}
									</text>
								</view>
								<u-line
									style="margin: 5px 0 2px"
									color="#dadbde"></u-line>
								<view
									style="
										display: flex;
										justify-content: flex-end;
									">
									<view class=""></view>
								<!-- 	<view
										v-if="
											item.status != '1' &&
											item.status != '2'
										"
										style="
											margin-right: 5px;
											padding: 5px 15px 5px 15px;
											border-radius: 5px;
											color: #fff;
											font-size: 12px;
											height: 20px;
											line-height: 20px;
											background-color: #0379ff;
											text-align: center;
											width: 40px;
										">
										开工
									</view> -->
									<view
										v-if="item.stoveStatus == '0'"
										@click="selectItem(item)"
										style="
											padding: 5px 15px 5px 15px;
											margin-right: 5px;
											border-radius: 5px;
											color: #fff;
											font-size: 12px;
											height: 20px;
											line-height: 20px;
											background-color: #0379ff;
											text-align: center;
											width: 40px;
										">
										上料
									</view>
									<!-- <view
										v-if="
											item.status != '1' ||
											item.status == '2'
										"
										style="
											padding: 5px 15px 5px 15px;
											border-radius: 5px;
											color: #fff;
											font-size: 12px;
											height: 20px;
											line-height: 20px;
											background-color: #0379ff;
											text-align: center;
											width: 40px;
										">
										结束
									</view> -->
								</view>
							</view>
						</view>
					</view>
				</u-list-item>
			</u-list>
			{{ mm }}
			<view class="flex justify-center" v-show="tableLoading">
				<u-loading-icon color="#000a7b"></u-loading-icon>
			</view>
			<!-- 空数据 -->
			<view v-show="tableList.length === 0 && !tableLoading">
				<u-empty
					mode="list"
					icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
			</view>
		</view>
		<!-- 工序选择器 -->
		<u-modal
			:show="showImport"
			ref="uModal"
			:content="`是否选择该加工单？`"
			confirmText="确认"
			showCancelButton
			@cancel="cancelImport"
			@confirm="confirmImport"></u-modal>
		<u-picker
			:show="show"
			:columns="columns"
			title="工序"
			keyName="processName"
			immediateChange
			cancel-text="取 消"
			@cancel="show = false"
			@confirm="confirmBtn"></u-picker>
	</view>
</template>

<script lang="ts" setup>
import { ref, computed, toRaw, onMounted, onUnmounted } from "vue";
import { onShow, onPullDownRefresh, onLoad, onUnload } from "@dcloudio/uni-app";
import { http } from "@/utils/http";
import { PageResult, TabInfo } from "@/types/type";
import { getSecondTime } from "@/utils/common";
import { useDictionaryStore } from "@/stores/useDictionaryStore";

import { useProduceStore } from "@/stores/useProduceStore";

// 实时获取
const workOrderList = computed(
	() => JSON.parse(useProduceStore().workOrderList) || []
);

const isLandscape = ref(false);

function checkOrientation() {
	const windowWidth = uni.getSystemInfoSync().windowWidth;
	const windowHeight = uni.getSystemInfoSync().windowHeight;
	isLandscape.value = windowWidth > windowHeight;
}

function handleResize(res) {
	// 当窗口尺寸发生变化时调用此函数
	console.log("resize:", res.size.windowWidth, res.size.windowHeight);
	checkOrientation();
}

onMounted(() => {
	// 页面加载完成时检查一次方向
	checkOrientation();
	// 监听窗口尺寸变化
	uni.onWindowResize(handleResize);
});

onUnmounted(() => {
	// 组件卸载时移除监听器
	uni.offWindowResize(handleResize);
});

// 待完成数据
const tableList = ref([]);
const tableLoading = ref(false);
const current = ref(0);
const totalPage = ref(0);
const total = ref(0);
//列表高度
const listHeight = ref("");
const mm = ref();

onLoad(() => {
	// #ifdef H5
	listHeight.value = "calc(100vh - 88px)";
	// #endif

	// #ifdef APP-PLUS
	listHeight.value = "calc(100vh - 48px)";
	plus.screen.lockOrientation("landscape");
	// #endif

	// 获取工序
	// getProcessList()
	// tableList.value = demo;
	// 监听窗口大小变化事件
	loadmore()
});

onShow(()=>{
	loadmore()
})


onUnload(() => {
	// #ifdef APP-PLUS
	plus.screen.lockOrientation("portrait");
	// #endif
});

onPullDownRefresh(() => {
	searchList();
	uni.stopPullDownRefresh();
});

async function loadmore(type?: string) {
	try {
		current.value += 1;
		tableLoading.value = true;
		const result: PageResult<any> = await http({
			method: "POST",
			url: "/mes/mesplanworkingform/queryByPage",
			data: {
				current: current.value,
				size: 20,
				data: {
					processingOrderNumber: searchText.value,
				processesClassification
				: 
				"3"
				},
			},
		}).finally(() => {
			tableLoading.value = false;
		});
		const list = result.data;
		for (let i = 0; i < list.length; i++) {
			tableList.value.push(list[i]);
		}
		totalPage.value = result.page;
		total.value = result.total;
		type === "refresh" && uni.stopPullDownRefresh();
	} catch (e) {
		console.log(e);
	}
}
const scrolltolower = () => {
	totalPage.value > current.value && loadmore();
};

// 点击扫码
const searchText = ref("");

// 列表搜索
function searchList() {
	current.value = 0;
	tableList.value = [];
	loadmore("refresh");
}

function clearList() {
	console.log("点击");
	searchText.value = "";
	searchList();
}

// 网页跳转
// async function jump(item) {
// 	uni.navigateTo({
// 		url: `/pages/eam/equ/searchDeviceLedger/detail?assetId=${item.id}`,
// 		fail: () => {
// 			uni.showToast({
// 				title: "敬请期待",
// 				icon: "error",
// 			});
// 		},
// 	});
// }

// --------------------------------------------------------------------- 选择工序
// 获取工序列表
const processList = ref([]);
const getProcessList = async () => {
	await http({
		method: "GET",
		url: "/eam/equAssetInfo/getList",
		data: {},
	}).then((res) => {
		columns.value[0] = res.data;
	});
};
// 假数据
let demo = [
	{
		id: "1",
		materialName: "空压机箱体~K152626~黑件",
		materialCode: "1234A",
		planBatch: 2402234,
		manufactureDate: "2025-01-15 12:12:12",
		productName: "飞轮壳～0490 8786R",
		heatNumber: "1号炉",
		planStart: "2025-01-12 12:12:12",
		planEnd: "2025-01-12 12:12:12",
		actualStartTime: "2025-01-15 12:12:12",
		actualEndTime: "2025-01-15 12:12:12",
		sort: "1",
		status: "1",
	},
	{
		id: "2",
		materialName: "空压机箱体~K152626~黑件",
		materialCode: "1234A",
		planBatch: 2402234,
		manufactureDate: "2025-01-15 12:12:12",
		productName: "飞轮壳～0490 8786R",
		heatNumber: "1号炉",
		planStart: "2025-01-12 12:12:12",
		planEnd: "2025-01-12 12:12:12",
		actualStartTime: "2025-01-15 12:12:12",
		actualEndTime: "2025-01-15 12:12:12",
		sort: "2",
		status: "2",
	},
	// {
	// 	id: "3",
	// 	materialName: "空压机箱体~K152626~黑件",
	// 	materialCode: "1234A",
	// 	planBatch: 2402234,
	// 	manufactureDate: "2025-01-15 12:12:12",
	// 	productName: "飞轮壳～0490 8786R",
	// 	heatNumber: "1号炉",
	// 	planStart: "2025-01-12 12:12:12",
	// 	planEnd: "2025-01-12 12:12:12",
	// 	actualStartTime: "2025-01-15 12:12:12",
	// 	actualEndTime: "2025-01-15 12:12:12",
	// 	sort: "3",
	// 	status: "3",
	// },
];
const processName = ref("");
const show = ref(false);
const columns = ref([[{ processName: "001工序", id: 1, processCode: "001" }]]);
// 打开选择
function processSelect() {
	show.value = true;
	uni.hideKeyboard();
}
// 确定选择
function confirmBtn(e) {
	if (e.value.length) {
		processName.value = e.value[0].processName;
		// getTabLiset(e);
	}
	show.value = false;
}
// 根据选择的工序获取列表
const getTabLiset = async (e) => {
	// loadmore();
};

// --------------------------------------------------------------------- 点击tableList item
const hh = ref([]);
const selectItem = (item) => {
	console.log(item);
	uni.navigateTo({
		url: `/pages/mes/meltingPlan/feedDetail?id=${item.id}`,
		fail: () => {
			uni.showToast({
				title: "敬请期待",
				icon: "error",
			});
		},
	});
	// hh.value = item;
	// showImport.value = true;
};
// 弹窗展示
const showImport = ref(false);

function cancelImport() {
	showImport.value = false;
}
function confirmImport() {
	showImport.value = false;
	useProduceStore().setWorkOrderList(toRaw(hh.value));
	console.log("workOrderList", workOrderList);
	uni.showToast({
		title: "选择成功",
	});
}
// --------------------------------------------------------------------- 工序
</script>

<style lang="scss" scoped>
uni-page-body {
	height: 100%;
	background-color: #fbfbff;
}
.search {
	:deep(.u-search__content) {
		box-shadow: 0 1px 8px 0px rgb(19 10 236 / 8%);
	}
}
// .baseForm {
// 	background-color: #fbfbff;
// 	.tab-bar {
// 		padding: 0 20px;
// 	}
// 	.upload-scan {
// 		padding: $uni-page-padding;
// 		padding-top: 5px;
// 		padding-bottom: 5px;
// 		display: flex;
// 		align-items: center;
// 		// box-shadow: 0 1px 10px 0px rgb(19 10 236 / 10%);
// 		.u-icon {
// 			margin-left: 8px;
// 		}
// 	}
// 	.u-line {
// 		padding: 10px 0;
// 	}
// }

// :deep(.input-box) {
// 	// width: calc(100% - 56px);
// 	width: 100%;
// 	// border: 0.5px solid #dadbde !important;
// 	border-radius: 100px;
// 	height: 32px;
// 	line-height: 32px;
// 	box-shadow: 0 1px 8px 0px rgb(19 10 236 / 8%);
// 	background-color: #fff;
// 	.u-input {
// 		pointer-events: none;
// 		height: 32px;
// 		line-height: 32px;
// 		padding-left: 10px !important;
// 	}
// 	.uni-input-placeholder {
// 		color: rgb(144, 147, 153) !important;
// 		font-size: 14px;
// 	}
// 	.u-icon__icon {
// 		font-size: 22px !important;
// 		line-height: 22px;
// 		font-weight: normal;
// 		top: 0px;
// 		color: rgb(144, 147, 153) !important;
// 	}
// 	image {
// 		padding: 6px 6px;
// 		width: 20px;
// 		height: 20px;
// 	}
// }

// :deep(.u-drawdown-item__content) {
// 	top: 88px !important;
// 	.common-search {
// 		display: flex;
// 		justify-content: flex-start;
// 		margin: 0 15px;
// 		padding: 13px 5px;
// 		border-bottom: 1px solid rgb(214, 215, 217);
// 	}
// }

// :deep(.dis-none){
// 		.u-dropdown__content{
// 			display: none;
// 		}
// 	}

@media (orientation: landscape) {
	.baseForm {
		background-color: #fbfbff;
		padding: 20px;
		.tab-bar {
			padding: 0 20px;
		}
		.upload-scan {
			padding: $uni-page-padding;
			padding-top: 5px;
			padding-bottom: 5px;
			display: flex;
			align-items: center;
			// box-shadow: 0 1px 10px 0px rgb(19 10 236 / 10%);
			.u-icon {
				margin-left: 8px;
			}
		}
		.u-line {
			padding: 10px 0;
		}
	}
	.list {
		.card {
			margin: $uni-page-padding;
			margin-top: 5px;
			margin-bottom: 4px;
			border-radius: 7px;
			padding: 12px 12px 0px 12px;
			transition: all 300ms ease;
			border: 1px solid #efefef;
			background-color: #fff;
			.card-flex {
				display: flex;
				align-items: flex-start;
			}

			.title {
				font-size: 14px;
				line-height: 40px;
				height: 40px;
				margin-top: 8px;
				font-weight: bold;
				color: #101010;
				border-top: 1px solid #e5ebff;
			}
			.uni-btn {
				float: right;
				font-size: 12px;
				height: 24px;
				line-height: 24px;
				padding: 0 15px;
				margin-top: 7px;
				font-weight: 400;
			}
			.des {
				font-size: 12px;
				line-height: 20px;
				margin-bottom: 3px;
			}
			.left {
				flex-basis: calc(50% + 10px);
			}
		}
	}

	.record-title {
		display: flex;
		justify-content: space-between;
		margin: 2px 0 2px 0;
		color: $primary-color;
		uni-text:nth-child(1) {
			font-weight: bold;
			font-size: 15px;
		}
		uni-text:nth-child(2) {
			font-weight: bold;
			font-size: 15px;
			min-width: 60px;
			text-align: right;
		}
	}
	.record-item {
		margin: 7px 0 !important;
		color: #8f8e94 !important;
		word-break: break-all;
		font-size: 13px;
	}
	.record-item1 {
		color: #8f8e94 !important;
		word-break: break-all;
		font-size: 13px;
	}
	.record-content .record-item > :first-child {
		min-width: 70px !important;
	}
}
@media (orientation: portrait) {
	.baseForm {
		background-color: #fbfbff;
		.tab-bar {
			padding: 0 20px;
		}
		.upload-scan {
			padding: $uni-page-padding;
			padding-top: 5px;
			padding-bottom: 5px;
			display: flex;
			align-items: center;
			// box-shadow: 0 1px 10px 0px rgb(19 10 236 / 10%);
			.u-icon {
				margin-left: 8px;
			}
		}
		.u-line {
			padding: 10px 0;
		}
	}
	.list {
		.card {
			margin: $uni-page-padding;
			margin-top: 5px;
			margin-bottom: 4px;
			border-radius: 7px;
			padding: 12px 12px 0px 12px;
			transition: all 300ms ease;
			border: 1px solid #efefef;
			background-color: #fff;
			.card-flex {
				display: flex;
				align-items: flex-start;
			}
			.right {
				flex: 1;
			}
			.title {
				font-size: 14px;
				line-height: 40px;
				height: 40px;
				margin-top: 8px;
				font-weight: bold;
				color: #101010;
				border-top: 1px solid #e5ebff;
			}
			.uni-btn {
				float: right;
				font-size: 12px;
				height: 24px;
				line-height: 24px;
				padding: 0 15px;
				margin-top: 7px;
				font-weight: 400;
			}
			.des {
				font-size: 12px;
				line-height: 20px;
				margin-bottom: 3px;
			}
			.left {
				flex-basis: calc(50% + 10px);
			}
		}
	}

	.record-title {
		display: flex;
		justify-content: space-between;
		margin: 2px 0 2px 0;
		color: $primary-color;
		uni-text:nth-child(1) {
			font-weight: bold;
			font-size: 15px;
		}
		uni-text:nth-child(2) {
			font-weight: bold;
			font-size: 15px;
			min-width: 60px;
			text-align: right;
		}
	}
	.record-item {
		margin: 7px 0 !important;
		color: #8f8e94 !important;
		word-break: break-all;
		font-size: 13px;
	}
	.record-item1 {
		color: #8f8e94 !important;
		word-break: break-all;
		font-size: 13px;
	}
	.record-content .record-item > :first-child {
		min-width: 70px !important;
	}
}
</style>
